#include <stdarg.h>
#include <stdio.h>
#include "game_version.h"
#include "game_thread.h"
#include "game_log.h"
#include "game_ev.h"
#include <unistd.h>
#include "socketd.h"

CGame::Logger::StdoutLoggerFactory g_LoggerFactory;
CGameLoggerFactory* CGameLoggerFactory::getInstance(){
  return &g_LoggerFactory;
}





void Socketd::onAccept(int fd, short events, void* arg){
  Client* cli = new Client(this);
  CGameSocketd::onAccept(cli,fd,events,arg);
}

BOOL Socketd::init(int port){
  _logger = CGameLoggerFactory::getInstance()->getLogger("Socketd");
  do{
    CC_BREAK_IF(!CGameSocketd::init(port));
    return TRUE;
  }while(true);
  return FALSE;
}

void Socketd::run(){
  do{
    CGameSocketd::run();
    usleep(40 * 1000);
  }while(true);
}

void Client::onEvents(bufferevent* bev,short events){
  _logger->info("onEvents %d",events);
  Connection::onEvents(bev,events);
}

void Client::onClose(){
  _logger->info("%s","onClose");
  delete this;
}

void Client::onRead(bufferevent* bev){
  _logger->info("%s","Client::onRead");
  Connection::onRead(bev);
  this->close();
}

BOOL Client::init(sockfd_t fd,event_base* evbase,const sockaddr_in& sa){
  _logger = CGameLoggerFactory::getInstance()->getLogger("Client");
  return Connection::init(fd,evbase,sa);
}

int main()
{
  __vprint();
  __ev_config();

  int ret = 0;

  do{

    CC_BREAK_IF(!g_LoggerFactory.init());

    Socketd sd;
    CC_BREAK_IF(!sd.init(9999));

    sd.run();

    return 0;
  }while(true);

  return ret;

  
  
}
